package com.system.service;


import com.system.controller.NotificationController;
import com.system.menu.UI;

import java.util.InputMismatchException;
import java.util.Map;
import java.util.Scanner;

public class NotificationService {
    private final UI ui = new UI();
    private final Scanner sc;
    private final NotificationController controller;
    private final CommentService cs;

    public NotificationService() {
        this.sc = new Scanner(System.in);
        this.controller = new NotificationController();
        this.cs = new CommentService();
    }

    public void notificationMenu(String email) {
        while (true) {
            this.ui.NotificationMenu(email);
            try {
                int optHistory = sc.nextInt();
                switch (optHistory) {
                    case 1:
                        notificationCheckMenu(email);
                        break;
                    case 2:
                        return;
                    default:
                        System.out.println("输入有误！");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

    public void notificationCheckMenu(String email) {
        int currentPage = 1;
        int totalPages = getTotalPages(email);
        while (true) {
            this.ui.NotificationCheckMenu(email);
            this.controller.getMapper().searchAll(currentPage, email);
            // 显示当前页和总页数
            printInfo(currentPage, totalPages);
            this.ui.NotificationButton();
            System.out.println("请输入你的选择：");
            try {
                int optAllTopics = sc.nextInt();
                switch (optAllTopics) {
                    case 1:
                        read(email);
                        break;
                    case 2:
                        this.cs.showCommentDeepZeroUtil(email,readLockID(email));
                        break;
                    case 3:
                        // 查看下一页
                        if (currentPage < totalPages) {
                            currentPage++;
                            System.out.println();
                            this.controller.getMapper().searchAll(currentPage, email);
                            // 显示当前页和总页数
                            printInfo(currentPage, totalPages);
                        } else {
                            System.out.println();
                            this.controller.getMapper().searchAll(currentPage, email);
                            // 显示当前页和总页数
                            printInfo(currentPage, totalPages);
                            System.out.println("已经是最后一页！");
                        }
                        break;
                    case 4:
                        // 查看上一页
                        if (currentPage > 1) {
                            currentPage--;
                            System.out.println();
                            this.controller.getMapper().searchAll(currentPage, email);
                            // 显示当前页和总页数
                            printInfo(currentPage, totalPages);
                        } else {
                            System.out.println();
                            this.controller.getMapper().searchAll(currentPage, email);
                            // 显示当前页和总页数
                            printInfo(currentPage, totalPages);
                            System.out.println("已经是第一页！");
                        }
                        break;
                    case 5:
                        return;
                    default:
                        System.out.println("输入有误，请重新选择！");
                        break;
                }
            } catch (InputMismatchException e) {
                System.out.println("输入错误，请输入数字！");
                sc.next(); // 清除错误输入
            }
        }
    }

    public void printInfo(int currentPage, int totalPages) {
        System.out.println(" \t\t\t\t\t\t\t当前页：" + currentPage + "  共 " + totalPages + " 页");
    }

    // 获取总页数--帖子
    public int getTotalPages(String email) {
        int totalRecords = this.controller.getMapper().countUnRead(email);  // 获取总记录数
        int pageSize = 5; // 每页显示5条
        return (int) Math.floor((double) totalRecords / pageSize); // 向上取整得到总页数
    }

    public void read(String email) {
        System.out.println("请输入序号：");
        try {
            int notionInput = sc.nextInt();
            // 检查输入的序号是否超出范围
            if (this.controller.getMapper().countUnRead(email) == 0) {
                System.out.println("没有任何未读消息！");
                return;
            }
            if (notionInput > this.controller.getMapper().countUnRead(email)) {
                System.out.println("输入错误，序号超出范围！");
                read(email); // 重新输入
            } else if (optionTopicID(email, notionInput)) {
                // 改成已读
                this.controller.getMapper().updateRead(email,notionInput);
            }
        } catch (InputMismatchException e) {
            System.out.println("输入错误，请输入数字！");
            sc.next(); // 清除错误输入
        }
    }
    public boolean optionTopicID(String email,int inputID) {
        Map<Integer, Integer> map = this.controller.getMapper().lockReplyID(email);
        return map.containsKey(inputID);
    }
    public int readLockID(String email) {
        System.out.println("请输入序号：");
        try {
            int notionInput = sc.nextInt();
            return optionTopicIDLock(email,notionInput);

        } catch (InputMismatchException e) {
            System.out.println("输入错误，请输入数字！");
            sc.next(); // 清除错误输入
        }
        return 0;
    }
    public int optionTopicIDLock(String email,int inputID) {
        Map<Integer, Integer> map = this.controller.getMapper().lockReplyID(email);
        return this.controller.getMapper().lockReplyIDFroALL(map.get(inputID));
    }
}

